package cn.edu.xhu.library_management.department.service.impl;

import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import cn.edu.xhu.library_management.department.entity.LmClass;
import cn.edu.xhu.library_management.department.entity.LmFaculty;
import cn.edu.xhu.library_management.department.entity.LmMajor;
import cn.edu.xhu.library_management.department.entity.search.SearchClass;
import cn.edu.xhu.library_management.department.entity.vo.ClassVo;
import cn.edu.xhu.library_management.department.mapper.ClassMapper;
import cn.edu.xhu.library_management.department.mapper.FacultyMapper;
import cn.edu.xhu.library_management.department.mapper.MajorMapper;
import cn.edu.xhu.library_management.department.service.ClassService;
import cn.edu.xhu.library_management.department.service.MajorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-02-02
 * @Description: 专业接口实现类
 */
@Service
public class ClassServiceImpl implements ClassService {
    @Resource
    private ClassMapper classMapper;
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private FacultyMapper facultyMapper;

    @Override
    public List<LmClass> findAllClasses() {
        return classMapper.findAllClasses();
    }

    @Override
    public List<ClassVo> findClassesByMajor(Integer maId) {
        List<LmClass> classesByMajor = classMapper.findClassesByMajor(maId);
        List<ClassVo> classVos = new ArrayList<>();
        classesByMajor.forEach(lmClass -> classVos.add(wrapClassVo(lmClass)));
        return classVos;
    }

    @Override
    public ClassVo findClassById(Integer clId) {
        // 班级ID查询班级
        LmClass classById = classMapper.findClassById(clId);
        if (classById == null) {
            return null;
        }
        return wrapClassVo(classById);
    }

    @Override
    public Map<String, Object> search(SearchClass searchClass) {
        // 没设置分页
        if (searchClass.getCurrentPage() != null && searchClass.getPageSize() != null) {
            searchClass.setCurrentPage((searchClass.getCurrentPage() - 1) * searchClass.getPageSize());
        }
        Map<String, Object> map = new HashMap<>();
        List<LmClass> lmClasses = classMapper.search(searchClass);
        // 转换成显示类
        List<ClassVo> classVos = new ArrayList<>();
        lmClasses.forEach(lmClass -> classVos.add(wrapClassVo(lmClass)));
        // 获取搜索的总条数
        Integer total = classMapper.searchTotal(searchClass);
        map.put("classes", classVos);
        map.put("total", total);

        return map;
    }

    @Override
    public boolean addClass(LmClass lmClass) {
        // 规则过滤
        addUpComRule(lmClass);
        return classMapper.addClass(lmClass) > 0;
    }

    @Override
    public boolean updateClass(LmClass lmClass) {
        // 规则过滤
        addUpComRule(lmClass);
        return classMapper.updateClass(lmClass) > 0;
    }

    @Override
    public boolean deleteClassById(Integer clId) {
        return classMapper.deleteClassById(clId) > 0;
    }

    @Override
    public boolean deleteClassesByIds(List<Integer> clIds) {
        return classMapper.deleteClassesByIds(clIds)>0;
    }


    /**
     * 添加修改共同规则
     * @param lmClass 班级对象
     * @return
     */
    public void addUpComRule(LmClass lmClass) {

//        System.out.println(lmClass);
        // 查找修改的专业ID是否存在，并且只有一个
        LmMajor major = majorMapper.findMajorById(lmClass.getMaId());
        if (major == null)
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR, "修改专业不存在");
        // 班级同名禁止添加
        LmClass findLmClass = classMapper.findClassByClassName(lmClass.getYear(), lmClass.getOrder(), lmClass.getMaId());
        if (findLmClass != null)
            throw new LibraryManagementException(ResultCode.ERROR, "该班级已存在");
    }

    /**
     * 包装班级显示类
     *
     * @param lmClass 班级对象
     * @return 班级显示对象
     */
    private ClassVo wrapClassVo(LmClass lmClass) {
        ClassVo classVo = new ClassVo();
        // 拷贝属性
        BeanUtils.copyProperties(lmClass, classVo);

        // 组装班级名称  入学年份(后两位) + 专业名称 + 班级序号
        // 获取入学年份(后两位)
        String year = classVo.getYear().toString().substring(classVo.getYear().toString().length() - 2);
        // 通过专业ID查询专业
        String majorName = majorMapper.findMajorById(lmClass.getMaId()).getName();
        classVo.setClassName(year + "-" + majorName + " " + classVo.getOrder() + "班");
        classVo.setMajorName(majorName);
        // 设置院系ID
        Integer faId = majorMapper.findMajorById(lmClass.getMaId()).getFaId();
        classVo.setFaId(faId);
        // 设置院系名称
        String faName = facultyMapper.findFacultyById(faId).getName();
        classVo.setFaName(faName);
        return classVo;
    }
}
